package com.zhike.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

import org.springframework.beans.factory.annotation.Value;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig {

    // @Bean
    // public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory
    // redisConnectionFactory) {
    // RedisTemplate<String, Object> template = new RedisTemplate<>();
    // template.setConnectionFactory(redisConnectionFactory);

    // // 设置 key 的序列化器为 StringRedisSerializer
    // StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    // template.setKeySerializer(stringRedisSerializer);
    // template.setHashKeySerializer(stringRedisSerializer);

    // // 可以根据需要设置 value 的序列化器
    // template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    // template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

    // template.afterPropertiesSet();
    // return template;
    // }

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.username}")
    private String username;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database}")
    private int database;

    private static Jedis jedis = null;
    private static JedisPool pool = null;

    @Bean
    public JedisPool jedisPool() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(8);
        poolConfig.setMaxIdle(8);
        poolConfig.setMinIdle(0);
        poolConfig.setMaxWait(Duration.ofMillis(10000));

        return new JedisPool(poolConfig, host, port, 2000, username, password, database);
    }

    

    // @Bean
    // public RedisTemplate<String, Object> redisTemplate() {
    // RedisTemplate<String, Object> template = new RedisTemplate<>();
    // template.setConnectionFactory(jedisConnectionFactory());
    // template.setKeySerializer(new StringRedisSerializer());
    // template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    // return template;
    // }
}
